using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Xml;

namespace GotLyrics.LyricGrabbers
{
    /// <summary>
    /// This is the scraper for the lyricsplugin website. 
    /// It will format the request url based on the artist and song title found in the <see cref="ILyricGrabber.Context"/> tags.
    /// The lyric is scraped from a div element with the id <i>lyrics</i>.
    /// </summary>
    public class LyricsPluginGrabber : LyricGrabber
    {
        //public LyricsPluginGrabber() { Enabled = false; }

        /// <summary>
        /// Formats the request address.
        /// </summary>
        /// <returns>
        /// The formated URL where to get the lyric from.
        /// </returns>
        /// <remarks>
        /// Use the <see cref="context"/> parameter to access tag information if you inherit from this class.
        /// </remarks>
        private string FormatRequestAddress(LyricGrabberContext context)
        {
            string artist = context.Tags.Artist;
            string title = context.Tags.Title;

            return String.Format("http://www.lyricsplugin.com/wmplayer03/plugin/?artist={0}&title={1}",
                                 HttpUtility.UrlEncode(artist), 
                                 HttpUtility.UrlEncode(title));
        }

        /// <summary>
        /// Parses the response from the website.
        /// </summary>
        /// <param name="response">The response from the website.</param>
        /// <returns>The parsed (scraped) lyric.</returns>
        /// <remarks>Return <c>null</c> or an empty string if the scrape wasn't able to locate the lyrics.</remarks>
        private string ParseResponse(string response)
        {
            string startTag = "<div id=\"lyrics\">";
            string endTag = "</div>";

            int start = response.IndexOf(startTag);

            if (start != -1)
            {
                start += startTag.Length;
                int end = response.IndexOf(endTag, start);

                if (end != -1)
                {
                    string lyric = response.Substring(start, end - start);
                    lyric = lyric.Replace("<br />", "");
                    lyric = lyric.Trim(" \n\r".ToCharArray());

                    if(lyric != string.Empty)
                        return lyric;
                }
            }

            return null;
        }

        /// <summary>
        /// The website of this lyrics provider. This is pointing to the main page on the website.
        /// </summary>
        /// <value></value>
        public override string WebSiteUrl
        {
            get { return "http://lyricsplugin.com/"; }
        }

        /// <summary>
        /// Grabs the lyric based on the current <see cref="Context"/> from the webservice.
        /// </summary>
        /// <param name="context"></param>
        /// <returns>
        /// The lyric if one was found, otherwise <c>null</c>.
        /// </returns>
        protected override string GrabLyric(LyricGrabberContext context)
        {
            string url = FormatRequestAddress(context);
            string response = WebClient.DownloadString(url);
            string lyrics = ParseResponse(response);

            return lyrics;
        }
    }
}
